<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Insert title here</title>
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/css/style.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/themes/default/easyui.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/themes/icon.css" />
		<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/css/easyui.icon.css" />
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/jquery.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery-easyui-1.4.3/locale/easyui-lang-zh_CN.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery.js/jquery.json.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/jquery.js/jquery.fw.ext.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/jquery.form.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/easyui.js/easyui.ext.js"></script>
		<!-- <script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/menu.js"></script> -->
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/message.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/MD5.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/moment.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath }/assets/js/zh-cn.js"></script>
    </head>
    <body>
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'north',split:true,border:false" style="height:50px;">
                <form id="searchForm">
                    <table>
                        <tr>
                        	<td>所属分类 <input class="easyui-combobox" style="width: 120px" id="parentIdSch" name="parentIdSch" /></td>
                            <td><a id="btn_add" class="easyui-linkbutton" data-options="group:'g1',iconCls:'icon-add'">添加</a></td>
                            <td><a id="btn_dele" class="easyui-linkbutton" data-options="group:'g1',iconCls:'icon-remove'">删除</a></td>
                            <td><a onclick="grid.datagrid('reload');" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-arrow_refresh'">刷新</a></td>
                        </tr>
                    </table>
                </form>
            </div>
            <div data-options="region:'center',border:false">
                <table id="grid">
                </table>
            </div>
            <div data-options="region:'east',split:true,border:false" style="width: 50%; padding: 5px;">
                <form id="form" name="form" method="post" class="form">
                    <fieldset>
                       <legend> 基本信息--<span id="operateMode_title">添加模式</span> </legend>
                       <input type="hidden" id="parentId" value="0" name="parentId" class="textbox" style="height: 22px;" />
                        <table class="table">
                            <tbody>
                                <tr>
                                    <th style="width:100px;">ID</th>
                                    <td><input id="id" readonly="readonly" name="id" class="textbox"  value=0  style="height: 22px; width:100px; background:#eee;" /></td>
                                </tr>
                                <tr>
                                    <th>产品类别名称</th>
                                    <td><input id="productTypeName" name="productTypeName" class="textbox easyui-validatebox" required="true"   style="height: 22px;" /></td>
                                </tr>
                                <tr>
                                	 <th>创建时间</th>
                                     <td>
                                        <input name="createdTime" class="easyui-datetimebox" style="width: 180px;" />
                                     </td>
                                </tr>      
                                <tr>
                                    <th>序号</th>
                                    <td><input id="sortId" name="sortId"
                                            class="easyui-validatebox easyui-numberspinner"
                                            data-options="min:0,max:100,required:true" value="0"   style="width: 50px;" />
                                    </td>
                                </tr>
                                <tr>
                                    <th>操作</th>
                                	<td><a id="operateMode_submit" class="easyui-linkbutton" data-options="iconCls:'icon-save'">确定</a> <a id="operateMode_reset" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">取消</a></td>
                                </tr>
                            </tbody>
                        </table>
                    </fieldset>
                </form>
            </div>
        </div>
        <script type="text/javascript">
            $(function() {
                var gird;
                var parentIdSch;
                var parentId = null;
                grid=$('#grid').datagrid({
                    fit:true,
                    border : false,
                    collapsible : false,
                    rownumbers : true,
                    pagination : true,
                    singleSelect : true,
                    rownumbers : true,
                    url:'${pageContext.request.contextPath }/listProducttype.do?parentIdSch=-1',
                    columns:[[
            		                       
            			{ sortable:true,field:'id',title:'ID'}
            		                       
            			,{ sortable:true,field:'productTypeName',title:'产品类别名称'}
            			
            			,{ sortable:true,field:'sortId',title:'序号'}
            		                       
            			,{ sortable:true,field:'createdTime',title:'创建时间',formatter: function(value, row, index){
	            			return value.substring(0,16);
	            		 },}
            		                       
            		    ]],
                    onLoadSuccess : function(data) {
                        $('.iconImg').attr('src', fw.pixel_0);
                    },
                    onClickRow : function(index, row) {
                    	$("#form").form("reset");
                        $("#form").form("load", row);
                        $("#operateMode_title").html("修改模式");
                    }
                });
              	//添加按钮事件
                $("#btn_add").click(function() {
                    $("#operateMode_title").html("添加模式");
                    $('#grid').datagrid("unselectAll");
                    $("#id").attr("value", 0);
                    $("#form").form("reset");
                    if(parentId){
                    	$("#parentId").val(parentId);
                    }else{
                    	$.messager.show({
                            title : message.title.normal,
                            msg : '请选择产品所属类型后再进行添加操作！！！',
                            timeout : message.timeout,
                            showType : message.showType
                        });
                    }
                })
            
                //删除按钮事件
                $("#btn_dele").click(function() {
                    var rows = $('#grid').datagrid('getSelections');
                    if (rows <= 0) {
                        $.messager.show({
                            title : message.title.normal,
                            msg : message.grid_select,
                            timeout : message.timeout,
                            showType : message.showType
                        });
                    } else {
                        $.messager.confirm(message.title.askTitle, message.dele_comfirm, function(r) {
                            if (r) {
                                var idValue = $("#form input[name='id']").val();
                                $.post("${pageContext.request.contextPath }/delProducttype.do", {
                                    id : idValue
                                }, function(result) {
                                    if (result.success) {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.dele_success,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                        grid.datagrid('reload');
                                    } else {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.dele_fail,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                    }
                                }, 'json');
                                $("#btn_add").click();
                            }
                        });
                    }
                })
            
                //确定按钮事件
                $("#operateMode_submit").click(function() {
                    var idValue = $("#form input[name='id']").val();
                    if ($('#form').form('validate')) {
                        if (idValue > 0) {
                            //修改
                            $.post("${pageContext.request.contextPath }/updateProducttype.do", fw.serializeObject($('form')), function(result) {
                                if (result.success) {
                                    $.messager.show({
                                        title : message.title.normal,
                                        msg : message.update_success,
                                        timeout : message.timeout,
                                        showType : message.showType
                                    });
            
                                    grid.datagrid("reload");
            
                                  //重新加载修改后的数据
                                    $("#form").form("load", "${pageContext.request.contextPath }/getProducttypeById.do?id=" + idValue);
                                }
                            }, 'json');
                        } else {
                            if(idValue.trim().length == 0){
                                $.messager.show({
                                    title : message.title.normal,
                                    msg : message.add_button_click,
                                    timeout : message.timeout,
                                    showType : message.showType
                                });
                            }else{
                                //新增
                                $.post("${pageContext.request.contextPath }/addAssistantProducttype.do", fw.serializeObject($('form')), function(result) {
                                	if (result.success) {
                                        $.messager.show({
                                            title : message.title.normal,
                                            msg : message.add_success,
                                            timeout : message.timeout,
                                            showType : message.showType
                                        });
                                        grid.datagrid("reload");
                                        $("#form").form("reset");
                                    }
                                }, 'json');
                            }
                        }
                    }
                })
                //重置操作
                $("#operateMode_reset").click(function() {
                    var idValue = $("#form input[name='id']").val();
                    if (idValue > 0) {
                        $("#form").form("load","${pageContext.request.contextPath }/getProducttypeById.do?id=" + idValue);
                    } else {
                        $("#form").form("reset");
                    }
                })
                
                
                //初始化产品主类
                $.getJSON('${pageContext.request.contextPath }/listAllProducttype.do?parentIdSch=0',function(productJson) {
   	            	if(productJson.rows.length==0){
   	            		parentIdSch = $('#parentIdSch').combobox();
   	            		parentIdSch.combobox("clear");
   	            	}else{
   	            		parentIdSch = $('#parentIdSch').combobox({
	   	 					editable : false,
	   	 					panelHeight : 'auto',
	   	 					valueField : 'id',
	   	 					textField : 'productTypeName',
	   	 					data:productJson.rows,
	   	 					DropDownStyle:'DropDownList',
	   	 					onSelect : function(record) {
	   	 						$("#parentId").val(record.id);
	   	 						parentId = record.id;
	   	 						$("#btn_add").click();
	   	 						$('#grid').datagrid({
	   	 		                     url: '${pageContext.request.contextPath }/listProducttype.do?parentIdSch='+parentId
	   	 		                });
	   	 						$('#grid').datagrid('load',{});
	   	 					},
	   	 					onLoadSuccess : function(data) {
	   	 						if (data.length > 0) {
	   	 							$('#parentIdSch').combobox('select', data[0].id);
	   	 						}
	   	 					}
	   	 				});
   	            	}
   	             }); 
            });
        </script>
    </body>
</html>